package cringer::grooveshark;

use XML::Simple;
use JSON;

# grooveshark.pm
# provides search functionality for cringer using the TinySong music search

our $VERSION = '2.0';

sub said{
 my $self = $_[0];
 my $args = $_[1];
 my $me = $self->{nick};
 
 unless($args->{body} =~ /^groove: ?(.*)$/){ # if this isn't a search query, we skip it.
  return;
 }

 my @r; # set up the response array
 my $request = $1;
 my $opt_l;
 if ($request =~ m/-l=([0-9]+)/){
  $opt_l = $1;
  $request =~ s/-l=([0-9]+) //;}
 print $request ."\n" . $opt_l ."\n";
 
 my @creds;
  open (CREDS, "creds.txt") || die "fill in your creds.txt.";
  while (my $line = <CREDS>){
   push(@creds, $line);}
  close(CREDS);
  $creds[0] =~ s/\n//;
  $creds[1] =~ s/\n//;
 
 $self->{ua}->credentials("posterous.com:80",'Posterous',$creds[0],$creds[1]); 

 $request =~ s/ $//;
 my $url, $string, $songinfo, $htmlresults, $title, $tags;

 if (defined($opt_l)) { 
  $url = URI->new( "http://tinysong.com/s/$request" );
  $url->query_form( 'limit' => $opt_l, 'format' => 'json' );
  $string ='';
  $songinfo = from_json($self->{ua}->get($url)->decoded_content); #get the right encoding 
 foreach my $line (@{$songinfo}) {
    $string .= $line->{SongID} . ",";}
  $string =~ s/,$//;

  $htmlresults = qq~ <object width="400" height="400"><param name="movie" value="http://listen.grooveshark.com/widget.swf" /><param name="flashvars" value="hostname=cowbell.grooveshark.com&amp;songIDs=$string&amp;style=metal&amp;p=0" /><embed src="http://listen.grooveshark.com/widget.swf" type="application/x-shockwave-flash" wmode="window" width="400" height="400" flashvars="hostname=cowbell.grooveshark.com&amp;songIDs=$string&amp;style=metal&amp;p=0"></embed>~;

  $title = "Playlist results for '$request'";
  $tags = $request;
  $tags =~ s/ /,/g;
  $tags .= ",Playlist"
  }
 else { 
   $url = URI->new( "http://tinysong.com/b/$request" );
   $url->query_form( 'format' => 'json' );
   $songinfo = from_json($self->{ua}->get($url)->decoded_content);
   $htmlresults = qq~ <object width="250" height="40"><param name="movie" value="http://listen.grooveshark.com/songWidget.swf" /><param name="flashvars" value="hostname=cowbell.grooveshark.com&amp;songID=$songinfo->{SongID}&amp;style=metal&amp;p=0" /><embed src="http://listen.grooveshark.com/songWidget.swf" type="application/x-shockwave-flash" wmode="window" width="250" height="40" flashvars="hostname=cowbell.grooveshark.com&amp;songID=$songinfo->{SongID}&amp;style=metal&amp;p=0"></embed>~;
#  $htmlresults =~ s/([^\\])(["`])/$1\\$2/g;
   $title = "$songinfo->{ArtistName} - $songinfo->{SongName}";  
   $tags = "$songinfo->{ArtistName},$songinfo->{SongName},$songinfo->{AlbumName}";}

 my $posturl = URI->new( "http://posterous.com/api/newpost");
  $posturl->query_form(  # And here the form data pairs:
   'site_id' => 1251953,
   'autopost' => 0,
   'title' => $title,
   'body' => $htmlresults,
   'tags' => $tags
  );

 my $data = XMLin($self->{ua}->get($posturl)->content);
 print $data->{post}->{url};
 my $posterousurl = $data->{post}->{url};

 if (defined(opt_l)){
  push(@r, qq~Listen to "$title" at $posterousurl~);}
 else { 
  push(@r, qq~Listen to "$songinfo[2]" by $songinfo[4] at $posterousurl~); }
return @r;
}
